package com.qyhl.webtv.module_circle.circle.friend;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.basiclib.utils.glide.GlideCircleTransform;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.inter.HomeActivityInterface;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.editbar.EditBar;
import com.qyhl.webtv.commonlib.utils.view.editbar.EditBarOnClickListenerImpl;
import com.qyhl.webtv.module_circle.R;
import com.qyhl.webtv.module_circle.R2;
import com.qyhl.webtv.module_circle.utils.itemview.ItemPictureView;
import com.qyhl.webtv.module_circle.utils.itemview.ItemTextView;
import com.qyhl.webtv.module_circle.utils.itemview.ItemVideoView;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2018/1/18.
 * 好友动态
 */
public class FriendMomentFragment extends BaseFragment implements FriendMomentContract.FriendView, BaseActivity.InputListener {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.editbar)
    EditBar editbar;

    private boolean editbarEnable = true;
    private boolean isVisibleToUser = false;
    private RecyclerView recRecycleview;

    private View view;

    private FriendMomentPresenter mPresenter;
    private HeaderAndFooterWrapper mHeaderAndFooterWrapper;
    private MultiItemTypeAdapter mAdapter;

    private CommonAdapter recAdapter;

    private List<CircleHomeBean.User> recList;

    private List<CircleHomeBean> mDataList;

    private String lastId = "0";

    private CircleHomeBean mBean;
    private int postion, uPos;
    private String date, content;

    private LoadingDialog.Builder builder;


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.circle_fragment_friend_moment, null);
    }

    @Override
    protected void create() {
        mPresenter = new FriendMomentPresenter(this);
        BusFactory.getBus().register(this);
        initView();
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        Log.e("circle", "initImmersionBar");
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    HomeActivityInterface homeActivityInterface;

    public void setHomeActivityInterface(HomeActivityInterface homeActivityInterface) {
        this.homeActivityInterface = homeActivityInterface;
    }

    public static FriendMomentFragment newInstance(HomeActivityInterface homeActivityInterface) {
        FriendMomentFragment friendMomentFragment = new FriendMomentFragment();
        friendMomentFragment.setHomeActivityInterface(homeActivityInterface);
        return friendMomentFragment;
    }

    /**
     * 加载数据
     */
    private void loadData() {
        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
            @Override
            public void login(boolean isLogin) {
                if (isLogin) {
                    mPresenter.getRecFriends();
                } else {
                    loadMask.setStatus(LoadingLayout.Empty);
                    loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                    loadMask.setEmptyText("尚未登录，快去登录吧！");
                    refresh.finishRefresh();
                    recyclerView.setAdapter(mAdapter);
                }
            }

            @Override
            public void error(String s) {
                loadMask.setStatus(LoadingLayout.Empty);
                loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                loadMask.setEmptyText(s);
                refresh.finishRefresh();
            }
        });

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
        } else {
            GSYVideoManager.releaseAllVideos();
        }
        if (editbar != null && editbar.getVisibility() == View.VISIBLE) {
            editbar.clearContent();
            editbar.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化界面
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        builder = new LoadingDialog.Builder(getActivity());
        builder.setTitle("提交中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);

        recList = new ArrayList<>();

        mDataList = new ArrayList<>();

        editbar.hideShare(true);
        editbar.hideComment(true);
        editbar.hideCollect(true);

        //初始化刷新
        refresh.setEnableLoadMore(true);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));

        recRecycleview = (RecyclerView) LayoutInflater.from(getActivity()).inflate(R.layout.circle_friend_moment_headview, null);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recRecycleview.setLayoutManager(linearLayoutManager);

        //初始化推荐好友列表
        recRecycleview.setAdapter(recAdapter = new CommonAdapter<CircleHomeBean.User>(getContext(), R.layout.circle_item_circle_rec_friend, recList) {

            @Override
            protected void convert(ViewHolder holder, final CircleHomeBean.User user, final int position) {
                ImageView headIcon = holder.getView(R.id.head_icon);
                TextView nickname = holder.getView(R.id.nickName);
                final ImageView followBtn = holder.getView(R.id.follow_btn);

                Glide.with(getActivity().getApplicationContext())
                        .load(user.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default).transform(new GlideCircleTransform(getContext())))
                        .into(headIcon);

                nickname.setText(user.getNickName());

                if (user.isFollow()) {
                    followBtn.setImageResource(R.drawable.circle_home_follow_on);
                } else {
                    followBtn.setImageResource(R.drawable.circle_home_follow_off);
                }
                followBtn.setEnabled(true);
                followBtn.setOnClickListener(v -> {
                    //防止重复点击
                    followBtn.setEnabled(false);
                    //如果是未关注
                    if (followBtn.getDrawable().getCurrent().getConstantState().equals(ContextCompat.getDrawable(getContext(), R.drawable.circle_home_follow_off).getConstantState())) {
                        mPresenter.doFollow(user.getUsername(), followBtn, position);
                    } else {
                        mPresenter.undoFollow(user.getUsername(), followBtn, position);
                    }
                });
            }
        });


        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.circle_recycleview_divider));
        recyclerView.addItemDecoration(divider);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mAdapter = new MultiItemTypeAdapter(getContext(), mDataList);
        mAdapter.addItemViewDelegate(new ItemPictureView(getActivity(), true));
        mAdapter.addItemViewDelegate(new ItemTextView(getActivity(), true));
        mAdapter.addItemViewDelegate(new ItemVideoView(getActivity(), true));

        mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);
        mHeaderAndFooterWrapper.addHeaderView(recRecycleview);
        recyclerView.setAdapter(mHeaderAndFooterWrapper);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        if (homeActivityInterface != null) {
            homeActivityInterface.addInputListener(this);
        }
        //推荐好友点击
        recAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("dstusername", recList.get(position).getUsername());
                RouterManager.router(ARouterPathConstant.CIRCLE_USER, bundle);


                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.CIRCLEOTHERUSER);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                int mPos;
                if (recList != null && recList.size() > 0) {
                    mPos = position - 1;
                } else {
                    mPos = position;
                }
                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(mPos).getId() + "");
                RouterManager.router(ARouterPathConstant.CIRCLE_DETAIL, bundle);
                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.CIRCLEDETAIL);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        //刷新
        refresh.setOnRefreshListener(refreshlayout -> {
            lastId = "0";
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        mPresenter.getRecFriends();
                    } else {
                        loadMask.setStatus(LoadingLayout.Empty);
                        loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                        loadMask.setEmptyText("尚未登录，快去登录吧！");
                        refresh.finishRefresh();
                    }
                }

                @Override
                public void error(String s) {
                    loadMask.setStatus(LoadingLayout.Empty);
                    loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                    loadMask.setEmptyText(s);
                    refresh.finishRefresh();
                }
            });
        });

        //加载更多
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                    @Override
                    public void login(boolean isLogin) {
                        if (isLogin) {
                            mPresenter.getContent(lastId);
                        } else {
                            loadMask.setStatus(LoadingLayout.Empty);
                            loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                            loadMask.setEmptyText("尚未登录，快去登录吧！");
                            refresh.finishRefresh();
                        }
                    }

                    @Override
                    public void error(String s) {
                        loadMask.setStatus(LoadingLayout.Empty);
                        loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                        loadMask.setEmptyText(s);
                        refresh.finishRefresh();
                    }
                });
            }
        });

        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                lastId = "0";
                CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                    @Override
                    public void login(boolean isLogin) {
                        if (isLogin) {
                            mPresenter.getRecFriends();
                        } else {
                            loadMask.setStatus(LoadingLayout.Empty);
                            loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                            loadMask.setEmptyText("尚未登录，快去登录吧！");
                            refresh.finishRefresh();
                        }
                    }

                    @Override
                    public void error(String s) {
                        loadMask.setStatus(LoadingLayout.Empty);
                        loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                        loadMask.setEmptyText(s);
                        refresh.finishRefresh();
                    }
                });
            }
        });

        //文本框
        editbar.setEditBarOnClickListener(new EditBarOnClickListenerImpl() {
            @Override
            public void editClick() {
                homeActivityInterface.setTabbarVisibility();
            }

            @Override
            public void onSend() {
                if (editbarEnable) {
                    editbarEnable = false;
                    CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                        @Override
                        public void login(boolean isLogin) {
                            if (isLogin) {
                                content = editbar.getContent();
                                String dstUsername;
                                if (StringUtils.isNotEmpty(content)) {
                                    if (uPos == -1) {
                                        dstUsername = "";
                                    } else {
                                        dstUsername = mBean.getPostList().get(uPos).getUser().getUsername();
                                    }
                                    builder.show();
                                    date = DateUtils.getCurrentTime();
                                    mPresenter.sendComment(mBean.getId() + "", content, dstUsername);
                                    editbar.clearContent();
                                } else {
                                    Toasty.normal(getContext(), "评论不能为空！").show();
                                    editbarEnable = true;
                                }
                            } else {
                                Toasty.normal(getContext(), "尚未登录或登录已失效！").show();
                                RouterManager.routerLogin(getContext(), 0);
                                editbarEnable = true;
                            }
                        }

                        @Override
                        public void error(String s) {
                            Toasty.normal(getActivity(), s).show();
                            editbarEnable = true;
                        }
                    });
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (RecyclerView.SCROLL_STATE_DRAGGING == newState && editbar.getVisibility() == View.VISIBLE) {
                    editbar.clearContent();
                    editbar.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 推荐好友列表
     *
     * @param beanList
     */
    @Override
    public void setRecFriends(List<CircleHomeBean.User> beanList) {
        recyclerView.setAdapter(mHeaderAndFooterWrapper);
        recList.clear();
        recList.addAll(beanList);
        recAdapter.notifyDataSetChanged();
        mPresenter.getContent(lastId);
    }

    /**
     * 好友动态
     *
     * @param beanList
     * @param isMore
     */
    @Override
    public void setContent(List<CircleHomeBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~~");
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);

        lastId = beanList.get(beanList.size() - 1).getId() + "";

        if (isMore) {
            mDataList.addAll(beanList);
        } else {
            mDataList.clear();
            mDataList.addAll(beanList);
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 推荐列表出错
     */
    @Override
    public void setRecError() {
        recyclerView.setAdapter(mAdapter);
        mPresenter.getContent(lastId);
    }

    /**
     * 无网络
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        loadMask.setReloadButtonText("点击重试~~");
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        loadMask.setReloadButtonText("点击重试~~");
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        loadMask.setReloadButtonText("点击重试~~");
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setEmptyImage(R.drawable.circle_friend_empty);
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);

    }

    /**
     * @param msg
     */
    @Override
    public void setLog(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        Log.i("info", msg);
    }

    @Override
    public void showMessage(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        Toasty.normal(getContext(), msg).show();
    }

    /**
     * 关注成功
     *
     * @param imageView
     */
    @Override
    public void followSuccess(ImageView imageView, int position) {
        imageView.setEnabled(true);
        imageView.setImageResource(R.drawable.circle_home_follow_on);
        recList.get(position).setFollow(true);
    }

    /**
     * 关注失败
     *
     * @param msg
     */
    @Override
    public void followError(String msg, ImageView imageView) {
        imageView.setEnabled(true);
        Toasty.normal(getContext(), msg).show();
    }

    /**
     * 取消成功
     *
     * @param imageView
     */
    @Override
    public void unFollowSuccess(ImageView imageView, int position) {
        imageView.setEnabled(true);
        imageView.setImageResource(R.drawable.circle_home_follow_off);
        recList.get(position).setFollow(false);
    }

    /**
     * 取消失败
     *
     * @param msg
     */
    @Override
    public void unFollowError(String msg, ImageView imageView) {
        imageView.setEnabled(true);
        Toasty.normal(getContext(), msg).show();
    }

    @Override
    public void commentSuccess(int postId, String msg, int code) {
        Toasty.normal(getContext(), msg).show();
        builder.dismiss();
        //0评论  1回复
        CircleHomeBean.PostList postBean;
        String nickName = CommonUtils.getInstance().getNickName();
        String userName = CommonUtils.getInstance().getUserName();
        String headicon = CommonUtils.getInstance().getUserLogo();
        if (code == 0) {
            postBean = new CircleHomeBean.PostList(date, postId, 1, content, new CircleHomeBean.User(nickName, headicon, userName, false), mBean.getUser());
        } else {
            postBean = new CircleHomeBean.PostList(date, postId, 2, content, new CircleHomeBean.User(nickName, headicon, userName, false), mBean.getPostList().get(uPos).getUser());
        }
        if (mBean.getPostList() != null && mBean.getPostList().size() >= 4) {
            mBean.getPostList().add(0, postBean);
            mBean.getPostList().remove(3);
        } else if (mBean.getPostList() == null) {
            List<CircleHomeBean.PostList> beanList = new ArrayList<>();
            beanList.add(postBean);
            mBean.setPostList(beanList);
        } else {
            mBean.getPostList().add(0, postBean);
        }
        mBean.setPostCount(mBean.getPostCount() + 1);
        mDataList.set(postion, mBean);
        mAdapter.notifyDataSetChanged();
        editbar.clearContent();
        editbar.setVisibility(View.GONE);
        editbarEnable = true;
        String integralSwitch = CommonUtils.getInstance().getIntergralSwitch();
        if (integralSwitch.equals("1")) {
            getCommentCoin();
        }
    }

    @Override
    public void commentError(String msg) {
        builder.dismiss();
        Toasty.normal(getContext(), msg).show();
        editbarEnable = true;
    }


    /**
     * 评论获取金币
     */
    private void getCommentCoin() {
        mPresenter.getCoin(getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
        homeActivityInterface.removeInputListener(this);
    }


    /**
     * eventBus 接收器
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.LoginMessage event) {
        if (event != null) {
            if (event.isLogin()) {//登录
                loadMask.setStatus(LoadingLayout.Loading);
                mPresenter.getRecFriends();
            } else {//注销
                lastId = "0";
                loadMask.setStatus(LoadingLayout.Empty);
                loadMask.setEmptyImage(R.drawable.circle_friend_empty);
                loadMask.setEmptyText("尚未登录，快去登录吧！");
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void praiseMessage(Event.praiseRefreshEvent event) {
        if (event != null) {
            int topicId = event.getTopicId();
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).getId() == topicId) {
                    if (event.getType() == 1) {
                        mDataList.get(i).setLove(true);
                        mDataList.get(i).setLoveCount(mDataList.get(i).getLoveCount() + 1);
                    } else {
                        mDataList.get(i).setLove(false);
                        mDataList.get(i).setLoveCount(mDataList.get(i).getLoveCount() - 1);
                    }
                }
            }
            mAdapter.notifyDataSetChanged();
        }
    }


    /**
     * 评论事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void commentMessage(Event.CommentEvent event) {
        if (event != null) {
            homeActivityInterface.setTabbarVisibility();
            editbar.setVisibility(View.VISIBLE);
            editbar.changeStateSend(getActivity());
            mBean = event.getBean();
            postion = event.getPostion();
            uPos = event.getuPos();
            if (uPos < 0) {
                editbar.setEditTextHint("评论");
            } else {
                editbar.setEditTextHint("回复:" + mBean.getPostList().get(uPos).getUser().getNickName());
            }
        }
    }


    /**
     * 双击返回顶部事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void backToTop(Event.backToTopEvent event) {
        if (event != null) {
            if (event.getType() == 2) {
                recyclerView.scrollTo(0, 0);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Event.circleRefresh event) {
        if (event != null && mDataList != null) {
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).getId() == event.getTopicId()) {
                    mDataList.remove(i);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void showkeyboard() {
        if (isVisibleToUser) {
            editbar.getEditText().requestFocus();
        }
    }

    @Override
    public void hideKeyboard() {
        if (isVisibleToUser) {
            editbar.getEditText().clearFocus();
        }
    }
}
